﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Yuans.BinaryConfig;
using Yuans.BinaryConfig.BlobIO;
using Yuans.BinaryConfig.Utils;

namespace Yuans.BinaryConfig.OperationBoardControl
{
    public partial class BlobItemOperationBoard : UserControl
    {
        private byte[] mBlobValue;
        private byte[] mImportDataBlobValue;

        public byte[] BlobValue
        {
            get { return mBlobValue; }
            set
            {
                mBlobValue = value;
                if (mBlobValue != null)
                {
                    currentBlobHashValue.Text = String.Format("{0:X}", HashMethod.SuperFastHash(mBlobValue));
                }
            }
        }

        public byte[] ImportDataBlobValue
        {
            get
            {
                return mImportDataBlobValue;
            }
            set
            {
                mImportDataBlobValue = value;
                if (mImportDataBlobValue != null)
                {
                    importBlobHashValue.Text = String.Format("{0:X}", HashMethod.SuperFastHash(mImportDataBlobValue));
                }

            }
        }

        private string mImportFileName;
        private string mExportFileName;


        public string ImportFileName
        {
            get { return mImportFileName; }
            set { mImportFileName = value; importFileNameTextBox.Text = value; }
        }
        public BlobItemOperationBoard()
        {
            InitializeComponent();

            importFileNameTextBox.DataBindings.Add("Text", this, "ImportFileName", true, DataSourceUpdateMode.OnValidation);
        }

        private void importFileButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog importFileDialog = new OpenFileDialog();
            importFileDialog.Filter = "TAS57XX配置文件|*.cfg|力度曲线文件|*.cuv|简单HEX文本|*.txt|所有文件|*.*";
            importFileDialog.FileName = "blob";

            if (importFileDialog.ShowDialog() != DialogResult.Cancel)
            {
                ImportFileName = importFileDialog.FileName;

                BlobValueIOManager blobValueIOManager = new BlobValueIOManager();
                blobValueIOManager.ImportFile(mImportFileName);
                ImportDataBlobValue = blobValueIOManager.BlobValue;
            }

        }

        private void exportFileButton_Click(object sender, EventArgs e)
        {
            SaveFileDialog exportFileDialog = new SaveFileDialog();
            exportFileDialog.Filter = "二进制文件|*.bin|所有文件|*.*";
            exportFileDialog.FileName = "blob";

            if (exportFileDialog.ShowDialog() != DialogResult.Cancel)
            {
                mExportFileName = exportFileDialog.FileName;

                BlobValueIOManager blobValueIOManager = new BlobValueIOManager();
                blobValueIOManager.BlobValue = mBlobValue;
                blobValueIOManager.ExportFile(mExportFileName);
            }
        }

        private void submitHexTestBtn_Click(object sender, EventArgs e)
        {
            ImportDataBlobValue = PlainHexTextBlobValueIO.ParseLineToBlob(quickInputTextBox.Text);
            ImportFileName = "快速输入的数据";
        }
    }
}
